package com.example.springexcel.Excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.example.springexcel.CopyPojo.CopyDataWrangling;
import com.example.springexcel.pojo.Vo.DataWranglingDataVo;
import com.example.springexcel.pojo.Vo.SequenceExcelDataVo;
import com.example.springexcel.pojo.dto.DataWranglingData;
import com.example.springexcel.pojo.dto.RandomTypographyData;
import com.example.springexcel.pojo.dto.SequenceExcelData;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Slf4j
public class DataProcessing {

    // 排序
    public static List<DataWranglingDataVo> Sorting(String ReadFile) {
        OrderDataListener listener = new OrderDataListener();
        EasyExcel.read(ReadFile, listener)
                .sheet("数据整理")
                .headRowNumber(1) // 从第1行读取表头
                .doRead();
        // 获取解析结果
        List<DataWranglingData> orders = listener.getOrders();


        List<DataWranglingDataVo> dataWranglingDataVos = CopyDataWrangling.convertAllToVoList(orders);
        // 排序逻辑：先按 date 升序，再按 factory 升序
        dataWranglingDataVos.sort(Comparator.comparing(DataWranglingDataVo::getDate)
                        .thenComparing(DataWranglingDataVo::getFactory)
        );


//        // 处理数据
//        log.info("数据整理 读取到一条数据{}", JSONUtil.toJsonStr(input));
        return dataWranglingDataVos;
    }


    // 随机排版
    public static HashMap<String, List<String>> RandomTypography(String ReadFile) {
        List<String> input = new ArrayList<>();

        // 使用更简洁的EasyExcel API
        // 读取
        EasyExcel.read(ReadFile, RandomTypographyData.class, new PageReadListener<RandomTypographyData>(dataList -> {
            for (RandomTypographyData demoData : dataList) {
                input.add(demoData.getName());
            }
        })).sheet("随机排班").doRead();

        // 处理数据
        // 生成随机的日期序列
        HashMap<String, List<String>> stringListHashMap = new HashMap<>();
        String[] data = {"周一", "周二", "周三", "周四", "周五", "周六"};

        // 检查输入数据是否足够
        int totalCount = input.size();

        // 计算需要每天排满数量
        int LinedUp = totalCount / 6;

        // 创建一个默认种子的随机数生成器
        Random random = new Random();


        List<String> randomTypographyData1 = input.subList(0, LinedUp * 6);
        // 使用 Collections.shuffle() 方法来打乱列表
        Collections.shuffle(randomTypographyData1);
        for (int i = 0; i < data.length; i++) {
            List<String> randomTypographyData = randomTypographyData1.subList(i * LinedUp, (i + 1) * LinedUp);
            stringListHashMap.put(data[i], randomTypographyData);
        }

        List<String> randomTypographyData2 = input.subList(LinedUp * 6, totalCount);

        for (String randomTypographyData : randomTypographyData2) {
            String datum = data[random.nextInt(6)];
            ArrayList<String> randomTypographyData4 = new ArrayList<>(stringListHashMap.get(datum));
            randomTypographyData4.add(randomTypographyData);
            stringListHashMap.put(datum, randomTypographyData4);
        }
        return stringListHashMap;
    }

    // 序列填充
    public static List<SequenceExcelDataVo> SequenceFilling(String ReadFile) {
        List<SequenceExcelData> input = new ArrayList<>();
        // 使用更简洁的EasyExcel API
        // 读取
        EasyExcel.read(ReadFile, SequenceExcelData.class, new PageReadListener<SequenceExcelData>(input::addAll)).sheet("序列填充").doRead();


        // 处理数据（使用Stream API重构）
        return input.stream()
                .flatMap(sequenceExcelData ->
                        IntStream.range(0, sequenceExcelData.getCount())
                                .mapToObj(i -> SequenceExcelDataVo.builder()
                                        .name(sequenceExcelData.getName())
                                        .build()))
                .collect(Collectors.toList());

    }
}
